/**用于存储和管理框架当前打开页面的缓存 */
import Cupk2Request from "@/service/remoteRequests/Cupk2Request";
import type { DataResponse } from "@/types/commons/DataResponse";
import { RequestTypeEnum } from "@/types/commons/RequestTypeEnum";
import { defineStore } from "pinia";

export const useSystemConfigStore = defineStore("systemConfigStore", {
  state: () => ({
    sysTitle: "",
    sysVersion: "",
    footTitle: "",
    logoPath: "/images/logo.png",
    fileExnames: "",
    fileSize: 0,
  }),
  getters: {
    getLogoPath(): string {
      return this.logoPath;
    },
    getFileSize(): number {
      if (this.fileSize == 0) {
        getRemotParas("cupk2.config.file.length.max", (val) => {
          this.fileSize = val as number;
        });
      }
      return this.fileSize;
    },
    getSysTitle(): string {
      if (!this.sysTitle) {
        getRemotParas("cupk2.config.sys.title.head", (val) => {
          this.sysTitle = val as string;
        });
      }
      return this.sysTitle;
    },
    getFootTitle(): string {
      if (!this.footTitle) {
        getRemotParas("cupk2.config.sys.title.foot", (val) => {
          this.footTitle = val as string;
        });
      }
      return this.footTitle;
    },
    getSysVersion(): string {
      if (!this.sysVersion) {
        getRemotParas("cupk2.conf.server.version", (val) => {
          this.sysVersion = val as string;
        });
      }
      return this.sysVersion;
    },
    getFileExnames(): string {
      if (this.fileExnames == "") {
        getRemotParas("cupk2.config.file.exnames", (val) => {
          this.fileExnames = val as string;
        });
      }
      return this.fileExnames;
    },
  },
  actions: {},
});

function getRemotParas(paramkey: string, handle: (val: unknown) => void) {
  Cupk2Request.submit("api/current/config", RequestTypeEnum.post, null, {
    key: paramkey,
  })
    .then((response: DataResponse) => {
      handle(response.data);
    })
    .catch((msg: Error) => {
      console.error(msg.message);
    });
  return "";
}
